package com.td.module.system.service.growth;

import com.td.framework.common.pojo.PageResult;
import com.td.module.system.controller.admin.growth.vo.list.GrowthModuleListCreateReqVO;
import com.td.module.system.controller.admin.growth.vo.list.GrowthModuleListExportReqVO;
import com.td.module.system.controller.admin.growth.vo.list.GrowthModuleListPageReqVO;
import com.td.module.system.controller.admin.growth.vo.list.GrowthModuleListUpdateReqVO;
import com.td.module.system.convert.growth.GrowthModuleListConvert;
import com.td.module.system.dal.dataobject.growth.GrowthModuleListDO;
import com.td.module.system.dal.mysql.growth.GrowthModuleListMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.td.module.system.enums.ErrorCodeConstants.MODULE_LIST_NOT_EXISTS;

/**
 * 生长模型列 Service 实现类
 *
 * @author wangxx
 */
@Service
@Validated
public class GrowthModuleListServiceImpl implements GrowthModuleListService {

    @Resource
    private GrowthModuleListMapper moduleListMapper;

    @Override
    public Long createModuleList(GrowthModuleListCreateReqVO createReqVO) {
        // 插入
        GrowthModuleListDO moduleList = GrowthModuleListConvert.INSTANCE.convert(createReqVO);
        moduleListMapper.insert(moduleList);
        // 返回
        return moduleList.getId();
    }

    @Override
    public void updateModuleList(GrowthModuleListUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateModuleListExists(updateReqVO.getId());
        // 更新
        GrowthModuleListDO updateObj = GrowthModuleListConvert.INSTANCE.convert(updateReqVO);
        moduleListMapper.updateById(updateObj);
    }

    @Override
    public void deleteModuleList(Long id) {
        // 校验存在
        this.validateModuleListExists(id);
        // 删除
        moduleListMapper.deleteById(id);
    }

    private void validateModuleListExists(Long id) {
        if (moduleListMapper.selectById(id) == null) {
            throw exception(MODULE_LIST_NOT_EXISTS);
        }
    }

    @Override
    public GrowthModuleListDO getModuleList(Long id) {
        return moduleListMapper.selectById(id);
    }

    @Override
    public List<GrowthModuleListDO> getModuleListList(Collection<Long> ids) {
        return moduleListMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<GrowthModuleListDO> getModuleListPage(GrowthModuleListPageReqVO pageReqVO) {
        return moduleListMapper.selectPage(pageReqVO);
    }

    @Override
    public List<GrowthModuleListDO> getModuleListList(GrowthModuleListExportReqVO exportReqVO) {
        return moduleListMapper.selectList(exportReqVO);
    }

}
